package ac.svn.accesscontrol.ui;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.SwingUtilities;

import ac.svn.accesscontrol.manager.SVNInfoManager;
import ac.svn.accesscontrol.store.AccessUnit;
import ac.svn.accesscontrol.store.ISVNItem;
import ac.svn.accesscontrol.store.SVNGroup;
import ac.svn.accesscontrol.store.SVNLocation;
import ac.svn.accesscontrol.store.SVNUser;

/**
 * 
 */

/**
 * @author Enigmanic
 *
 */
public class LocationFilterByUser 
{
	private String searchCriteria = "";
	private Thread listerThread = null;
	private JList targetListView = null;
	private DefaultListModel listModel = null;
	private Set<ISVNItem> targetList = null;
	private boolean groupFiltered = false;
	
	public LocationFilterByUser(String componentId,String modelId)
	{
		targetListView = (JList) UIComponentLibrary.getComponent(componentId);
		listModel = (DefaultListModel) UIComponentLibrary.getComponent(modelId); 
		
		listerThread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				//Clear list before populating it.
				listModel.clear();
				if(true == groupFiltered)
				{
					filterByGroup();
				}
				else
				{
					filterByUser();
				}
				SwingUtilities.invokeLater(new Runnable() {
					
					@Override
					public void run() {
						SwingUtilities.updateComponentTreeUI(targetListView);						
					}
				});				
			}
			
			private void filterByUser()
			{
				SVNUser svnUser = SVNInfoManager.getInfoManager().getSvnUser(searchCriteria);
				for(ISVNItem item : targetList)
				{
					SVNLocation svnLocation = (SVNLocation) item;
					for(ISVNItem svnItem : svnLocation.getAccessUnits())
					{
						AccessUnit  accessUnit = (AccessUnit) svnItem;
						if(svnUser.getUserAlias().equalsIgnoreCase(accessUnit.getOwnerId()) ||
								svnUser.getGroups().contains(accessUnit.getOwnerId()))
						{
							listModel.addElement(svnLocation);
						}
					}
				}
			}
			
			private void filterByGroup()
			{
				SVNGroup svnGroup = SVNInfoManager.getInfoManager().getGroup(searchCriteria);
				for(ISVNItem item : targetList)
				{
					SVNLocation svnLocation = (SVNLocation) item;
					for(ISVNItem svnItem : svnLocation.getAccessUnits())
					{
						AccessUnit  accessUnit = (AccessUnit) svnItem;
						if(svnGroup.getGroupAlias().equalsIgnoreCase(accessUnit.getOwnerId()) ||
								svnGroup.getGroupMembers().contains(accessUnit.getOwnerId()))
						{
							listModel.addElement(svnLocation);
						}
					}
				}
			}
			
		});		
	}
	 
	public void filterListByUser(String userId)
	{
		groupFiltered = false;
		searchCriteria = userId;
		resetThread();
		targetList = SVNInfoManager.getInfoManager().getAllLocations();
		filter();
	}
	
	public void filterListByGroup(String groupId)
	{
		groupFiltered = true;
		searchCriteria = groupId;
		resetThread();
		targetList = SVNInfoManager.getInfoManager().getAllLocations();
		filter();
	}
	
	private void resetThread()
	{
		if(listerThread.isAlive())
		{
			listerThread.interrupt();
		}
	}
	
	private void filter()
	{
		listerThread.run();
	}
	
}
